﻿using System;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
namespace StudentManagement
{
	/// <summary>
	/// Interaction logic for SignIn.xaml
	/// </summary>
	public partial class SignIn : Window
	{
		string SASConnectionString = Properties.Settings.Default.SASConnectionString;
		SqlConnection conn;
		SqlCommand query;
	    DispatcherTimer closeTimer = new DispatcherTimer();

		public SignIn()
		{
			InitializeComponent();
			conn = new SqlConnection(SASConnectionString);
			conn.Open();
		}

		private void btnSignIn_Click(object sender, RoutedEventArgs e)
		{      
		    closeTimer.Start();
		}

        private void closeTimer_Tick(object sender,EventArgs e)
        {                      
            using (conn = new SqlConnection(SASConnectionString))
            {
                conn.Open();
                try
                {
                    query = new SqlCommand(String.Format("SELECT username,password FROM login WHERE username='{0}' and                                                                    password='{1}'", tbUsername.Text, pbPassword.Password), conn);

                    if (query.ExecuteReader().HasRows)
                    {
                        this.Hide();
                        AdminPanel ap = new AdminPanel();
                        ap.ShowDialog();
                        //formAdminPanel.ShowDialog();
                        this.Close();
                        closeTimer.Stop();
                    }
                    else
                    {
                        string error = "Username/password\n";
                        error += "not recognized";
                        lbSingInError.Content = error;
                        lbSingInError.Visibility = Visibility.Visible;
                        closeTimer.Stop();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                closeTimer.Stop();
            }
            
        }

		private void UpperBorder_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			this.DragMove();
		}

		private void btnSignIn_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
			{
				btnSignIn_Click(sender, e);
			}
		}

		private void tbPassword_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == Key.Enter && btnSignIn.IsEnabled == true)
			{
				btnSignIn_Click(sender, e);
			}
		}

		private void tbUsername_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
		{
			if (tbUsername.Text != String.Empty && pbPassword.Password.Length > 0)
			{
				btnSignIn.IsEnabled = true;
			}
			else
			{
				btnSignIn.IsEnabled = false;
			}
		}

		private void pbPassword_PasswordChanged(object sender, RoutedEventArgs e)
		{
			if (tbUsername.Text != String.Empty && pbPassword.Password.Length > 0)
			{
				btnSignIn.IsEnabled = true;
			}
			else
			{
				btnSignIn.IsEnabled = false;
			}
		}

		private void btnClose_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		private void btnMinimize_Click(object sender, RoutedEventArgs e)
		{
			this.WindowState = WindowState.Minimized;
		}

		private void tbUsername_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter && btnSignIn.IsEnabled == true)
			{
				btnSignIn_Click(sender, e);
			}
		}

        private void SignInWindow_Loaded(object sender, RoutedEventArgs e)
        {
			this.Hide();
			AdminPanel ap = new AdminPanel();
			ap.ShowDialog();
			//formAdminPanel.ShowDialog();
			this.Close();
			closeTimer.Stop();
            
            //closeTimer.Tick += new EventHandler(closeTimer_Tick);
            //closeTimer.Interval = new TimeSpan(0, 0, 0, 1);       
        }
	}
}
